#property copyright   "Copyright 2013,mingxinhe@gmail.com"
#property link "mingxinhe@gmail.com"

//---- indicator settings
#property  indicator_separate_window
#property  indicator_buffers 3 
#property  indicator_width1  2

extern string        IndicatorName = "Custom_Moving_Average";
extern int           FastMA_period = 7; 
extern int           FastMA_Shift     = 0; 
extern string        FastMA_MA_Method.Desc = "0-SMA,1-EMA,2-SMMA,3-LWMA,4-T3MA,5-JMA,6-JFATL,7-J2JMA,8-ParMA";
extern int           FastMA_MA_Method = 3;  
extern string        FastMA_Applied_Price.Desc = "0-T_MultipleHigh,1-R__MultipleLow";
extern int           FastMA_Applied_Price = 0;
extern double        FastMA_T_MultipleHigh = 1.001;
extern double        FastMA_R__MultipleLow = 0.999;
extern double        FastMA_T3_b_=70;                      //Coefficient x100  
extern int           FastMA_JMAPhase=100; //Smoothing parameter
extern int           FastMA_MA_period2 = 5; // Depth of the second smoothing                    
extern double        FastMA_JMAPhase2=100;  // Second smoothing parameter,

extern int           SlowMA_period = 10; 
extern int           SlowMA_Shift     = 0; 
extern string        SlowMA_MA_Method.Desc = "0-SMA,1-EMA,2-SMMA,3-LWMA,4-T3MA,5-JMA,6-JFATL,7-J2JMA,8-ParMA";
extern int           SlowMA_MA_Method = 3;  
extern string        SlowMA_Applied_Price.Desc = "0-T_MultipleHigh,1-R__MultipleLow";
extern int           SlowMA_Applied_Price = 0;
extern double        SlowMA_T_MultipleHigh = 1.01;
extern double        SlowMA_R__MultipleLow = 0.99;
extern double        SlowMA_T3_b_=70;                      //Coefficient x100  
extern int           SlowMA_JMAPhase=100; //Smoothing parameter
extern int           SlowMA_MA_period2 = 5; // Depth of the second smoothing                    
extern double        SlowMA_JMAPhase2=100;  // Second smoothing parameter,

extern int           MA_Period_4CandleLenth=10;
extern int           MAXMIN_Period = 10;
extern color         UpTrend_Color = Green;
extern color         DownTrend_Color = Red;
extern color         LINE = Yellow;

double     MABuffer[], HistBufferUp[], HistBufferDown[], HistBuffer[];

double max = -1, min = 1000000;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- drawing settings
   IndicatorBuffers(4);   
   SetIndexBuffer(0,HistBufferUp);  
   SetIndexBuffer(1,HistBufferDown); 
   SetIndexBuffer(2,MABuffer);
   SetIndexBuffer(3,HistBuffer);
   SetIndexStyle(0,DRAW_HISTOGRAM,EMPTY,EMPTY,UpTrend_Color);
   SetIndexStyle(1,DRAW_HISTOGRAM,EMPTY,EMPTY,DownTrend_Color);
   SetIndexStyle(2,DRAW_LINE,EMPTY,EMPTY,LINE);
   
   
   IndicatorDigits(Digits+1);
//---- indicator buffers mapping
//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("CustumerMAInd1("+FastMA_period+","+ SlowMA_period +","+ MA_Period_4CandleLenth +")");
   SetIndexLabel(0,"UpTrendDif");
   SetIndexLabel(1,"DownTrendDif");
   SetIndexLabel(2,"Average");
   
   
//---- initialization done
   return(0);
  }
  
void deinit() 
{
    ObjectDelete("max");
    ObjectDelete("min");
}


int start()
{
   int limit;
   int ExtCountedBars=IndicatorCounted();
//---- check for possible errors
   if (ExtCountedBars<0) return(-1);
//---- last counted bar will be recounted
   if (ExtCountedBars>0) ExtCountedBars--;
   limit=Bars-ExtCountedBars;
      
 //---- macd counted in the 1-st buffer
    for(int i= limit-1; i >= 0; i--)
    { 
        double fastMA = iCustom(NULL, 0, IndicatorName,  FastMA_period, FastMA_Shift, FastMA_MA_Method.Desc, FastMA_MA_Method, FastMA_Applied_Price.Desc, FastMA_Applied_Price, 
                               FastMA_T_MultipleHigh, FastMA_R__MultipleLow, FastMA_T3_b_, FastMA_JMAPhase, FastMA_T3_b_, FastMA_MA_period2, FastMA_JMAPhase2, 0, i);
        double slowMA = iCustom(NULL, 0, IndicatorName,  SlowMA_period, SlowMA_Shift, SlowMA_MA_Method.Desc, SlowMA_MA_Method, SlowMA_Applied_Price.Desc, SlowMA_Applied_Price, 
                               SlowMA_T_MultipleHigh, SlowMA_R__MultipleLow, SlowMA_T3_b_, SlowMA_JMAPhase, SlowMA_T3_b_, SlowMA_MA_period2, SlowMA_JMAPhase2, 0, i);   
                             
        if (fastMA > slowMA)
        {
            HistBufferUp[i] = MathAbs(High[i] - slowMA);            
            HistBuffer[i] = HistBufferUp[i];
        }
        else if (fastMA < slowMA)
        {                
            //double lowerBand = iEnvelopes(NULL, 0, ma_period, ma_method, ma_shift, applied_price, deviation,MODE_LOWER,i);                
            HistBufferDown[i] = MathAbs(Low[i] - slowMA);            
            HistBuffer[i] = HistBufferDown[i];
        }                 
    }
    
    for (i= limit-1; i >= 0; i--)
    {
        MABuffer[i] = iMAOnArray(HistBuffer, 0, MA_Period_4CandleLenth, 0, MODE_SMA, i);
    }
        
     //---- signal line counted in the 2-nd buffer
     
        max = HistBuffer[ArrayMaximum(HistBuffer, MAXMIN_Period, 0)];
        min = HistBuffer[ArrayMinimum(HistBuffer, MAXMIN_Period, 0)];
        
         if (ObjectFind("max") == -1)
         {
             int wnd = WindowFind("CustumerMAInd1("+FastMA_period+","+ SlowMA_period +","+ MA_Period_4CandleLenth +")");
             ObjectCreate("max", OBJ_LABEL, wnd, 0, 0);
             ObjectSet("max", OBJPROP_CORNER, 1);
             ObjectSet("max", OBJPROP_XDISTANCE, 10);
             ObjectSet("max", OBJPROP_YDISTANCE, 30);
             ObjectSetText("max", "Max:" + max, 10, "Times New Roman", Yellow);
         } 
         else
         {
             ObjectSetText("max", "Max:" + max, 10, "Times New Roman", Yellow);
         }
         
         if (ObjectFind("min") == -1)
         {
             wnd = WindowFind("CustumerMAInd1("+FastMA_period+","+ SlowMA_period +","+ MA_Period_4CandleLenth +")");
             ObjectCreate("min", OBJ_LABEL, wnd, 0, 0);
             ObjectSet("min", OBJPROP_CORNER, 1);
             ObjectSet("min", OBJPROP_XDISTANCE, 10);
             ObjectSet("min", OBJPROP_YDISTANCE, 50);
             ObjectSetText("min", "Min:" + min, 10, "Times New Roman", Yellow);
         } 
         else
         {
             ObjectSetText("min", "Min:" + min, 10, "Times New Roman", Yellow);
         } 
         
//---- done

    
   return(0);
 }
  